
/* Generated by Interface Builder */

#import <sys/time.h>
#import <appkit/Button.h>
#import <libc.h>
#import "ExpController.h"


@implementation ExpController


- startSquareHit
{
    struct timeval tp;
    struct timezone tzp;
    
    gettimeofday(&tp, &tzp);
    
    startTime = (tp.tv_sec & 0xffff) * 1000000 + tp.tv_usec;

    fprintf(stderr, "startSec:%d, startuSec:%d, startTime:%d\n", tp.tv_sec, tp.tv_usec, startTime);
    
    hitItem = random() % menuParms.num_items;
    
    [expMenuView setHitItem:hitItem];
    
    return self;
}

- menuItemHit:(int) item_num
{
    struct timeval tp;
    struct timezone tzp;
    long total;
    unsigned long end_time;

    gettimeofday(&tp, &tzp);    
    
    end_time = (tp.tv_sec & 0xffff) * 1000000 + tp.tv_usec;
   
    total = end_time - startTime;

    fprintf(stderr, "endSec:%d, enduSec:%d, end_time:%d, total:%d\n", tp.tv_sec, tp.tv_usec, end_time, total);

    fprintf(outputFile, "\t%.3f\t", ((float) total) / 1000000.0);

    fprintf(outputFile, "%s", hitItem == item_num ? "hit" : "miss");
	
    if ( menuParms.menu_type == VERTICAL )
    {
        float item_distance;
	
	item_distance = abs(menuParms.num_items/2 - item_num) *
	                 (menuParms.option2+menuParms.option3);		
        fprintf(outputFile, "\t\t%.2f\n", item_distance);        
	fprintf(stderr, "\t%.2f\n", item_distance);
    }
    else if ( menuParms.menu_type == CIRCLE_PIE )
        fprintf(outputFile, "\t\t%.2f\n", menuParms.option2);
    else
        fprintf(outputFile, "\t\t%.2f\n", menuParms.option3);

    if ( trialsToGo > 1 )
    {
        trialsToGo--;
        [expMenuView setStartState];
    }
    else
    {
        fprintf(stderr, "done experiment\n");
	fprintf(outputFile, "done experiment\n\n");
	fclose(outputFile);
	
	[statusText setStringValue:"DONE EXPERIMENT -- hit Ready to continue.\n"];
	[readyButton setEnabled:1];
    }

    return self;
}

- menuItemMissed
{
    struct timeval tp;
    struct timezone tzp;
    long total;
    unsigned long end_time;
    
    gettimeofday(&tp, &tzp);    

    end_time = (tp.tv_sec & 0xff) * 1000000 + tp.tv_usec;
   
    total = end_time - startTime;
    
    fprintf(stderr, "endSec:%d, enduSec:%d, end_time:%d, total:%d\n", tp.tv_sec, tp.tv_usec, end_time, total);

    fprintf(outputFile, "%.3f\t\tmiss\n", ((float) total) / 1000000.0);
    
    if ( trialsToGo > 1 )
    {
        trialsToGo--;
        [expMenuView setStartState];
    }
    else
    {
        fprintf(stderr, "done experiment\n");
	fprintf(outputFile, "done experiment.\n");
	fclose(outputFile);
    }

    return self;
}


- readyButtonHit:sender
{
    fprintf(stderr, "Ready button hit.\n");

    [sender setEnabled:0];
    
    if ( trialsToGo == 0 )
    {
        /* close this window and open control and results window */
    }
    else
        [expMenuView setStartState];
	
    return self;
}

- computeArea:(float *) area
{
    switch ( menuParms.menu_type )
    {
        case VERTICAL:
	case CIRCLE_RECTANGLE:
	    *area = menuParms.option1 * menuParms.option2;
	    break;
	    
	case CIRCLE_PIE:
	    *area = ((3.14159*(menuParms.option2+menuParms.option3) * 
	                          (menuParms.option2+menuParms.option3)) -
	                 (3.14159*menuParms.option2*menuParms.option2)) /
			                           (360.0/menuParms.option1);
	    break;
    }
    
    return self;
}


- performExperiment:(MVParms *) menu_parms :(int) num_trials :(char *) exp_id
{
    float item_area;
    
    outputFile = fopen(exp_id, "a+");
    
    srandom((int) time(NULL) );
    
    menuParms = *menu_parms;
    trialsToGo = num_trials;
    
    
    fprintf(outputFile, "***\nExperiment id:%s\n", exp_id);
    fprintf(outputFile, "# menu items:%d, # of trials:%d\n",
                              menu_parms->num_items, num_trials);

    if ( menu_parms->menu_type == VERTICAL )
    {
        fprintf(outputFile, "VERTICAL menu:\n\twidth:%.2f, height:%.2f, distance:%.2f\n",
	        menu_parms->option1, menu_parms->option2, menu_parms->option3);
    }
    else if ( menu_parms->menu_type == CIRCLE_PIE )
    {
        fprintf(outputFile, "CIRCULAR PIE menu:\n\tangle covered:%.2f, radius covered:%.2f, menu radius:%.2f\n", menu_parms->option1, menu_parms->option2,
	        menu_parms->option3);
    }
    else
        fprintf(outputFile, "CIRCULAR RECT menu:\n\twidth:%.2f, height:%.2f, radius:%.2f\n", menu_parms->option1, menu_parms->option2, menu_parms->option3);

    [self computeArea:&item_area];	
    fprintf(outputFile, "\tItem area: %.2f\n", item_area);
	
    fprintf(outputFile, "\nSelection Time\tHit/Miss\tDistance\n");
    fprintf(outputFile, "--------------\t--------\t--------\n");
        
    [expMenuView setMenuParms:&menuParms];

    [readyButton setEnabled:1];
        
    return self;
}


@end
